package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;
import utils.SingletonScanner;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User{

    private Library library = Library.getInstance();
    private Scanner scanner = SingletonScanner.getScanner();
    private int borrowCount;
    private PairOfUidAndBookId[] pairOfUidAndBookIds;
    private AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();


    public NormalUser(int id, String name){
        super(id, name, "普通用户");
        library.loadBooks();
    }
    @Override
    public int display() {
        System.out.println("普通用户:"+this.name+"的菜单");
        System.out.println("1. 查找书籍");
        System.out.println("2. 显示书籍");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅书籍");
        System.out.println("5. 归还书籍");
        System.out.println("6. 查看当前个人的借阅情况");

        System.out.println("请输入你的选择:");
        int choice = scanner.nextInt();
        scanner.nextLine();

        return choice;
    }

    public void borrowedBook() {
        System.out.println("普通用户的borrowedBook执行了");
        library.dispalyBook();
        System.out.println("请输入您要借阅的图书的id: ");
        int bookId = scanner.nextInt();

        loadUidAndBookId();

        Book book = library.searchBookByBid(bookId);
        if(book == null){
            System.out.println("图书馆没有你要借阅的这本书: " + book.getTitle());
            return;
        }else {
            for (int i = 0; i < borrowCount; i++) {
                PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
                if(book.getId() == pairOfUidAndBookId.getBookId()){
                    if(this.getId() == pairOfUidAndBookId.getUserId()){
                        System.out.println("图书: " + book.getTitle() + " 已经被你借了, 麻烦你借阅其他书籍");
                        return;
                    }else {
                        System.out.println("图书: " + book.getTitle() + " 已经被别人借了, 麻烦你借阅其他书籍");
                        return;
                    }
                }
            }
        }

        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(this.getId(), bookId);
        pairOfUidAndBookIds[borrowCount++] = pairOfUidAndBookId;
        storeUidAndBookId();

        library.borrowedBook(book);
    }

    public void returnBook() {
        System.out.println("普通用户的returnBook执行了");
        viewBorrowHistoryBook();
        System.out.println("请输入你要归还的书籍的id: ");
        int bookId = scanner.nextInt();

        Book book = library.searchBookByBid(bookId);

        if(book == null){
            System.out.println("图书管没有这本书: " + book.getTitle());
            return;
        }

        loadUidAndBookId();

        boolean flg = false;
        for (int i = 0; i < borrowCount; i++) {
            if(pairOfUidAndBookIds[i].getBookId() == bookId){
                flg = true;
                if(pairOfUidAndBookIds[i].getUserId() == this.getId()){
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowCount-1];
                    pairOfUidAndBookIds[borrowCount-1] = null;
                    borrowCount--;
                    storeUidAndBookId();

                    library.returnBook(book);

                }else {
                    System.out.println("这本书不是你借阅的, 麻烦你归还自己借阅的书籍");
                }
            }
        }
        if(!flg){
            System.out.println("这本书 " + book.getTitle() + " 没有被借阅, 请归还你借阅的书籍");
            return;
        }


    }

    public void viewBorrowHistoryBook() {
        System.out.println("普通用户的viewBorrowHistoryBook执行了");
        loadUidAndBookId();
        if(borrowCount == 0){
            System.out.println("没有借阅记录");
            return;
        }else {
            boolean flg = false;
            for (int i = 0; i < borrowCount; i++) {
                if(pairOfUidAndBookIds[i].getUserId() == this.getId()){
                    flg = true;
                    Book book = library.searchBookByBid(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg){
                System.out.println("当前用户: " + this.getId() + " 没有借阅图书");
            }
        }
    }



    public void loadUidAndBookId(){
        try {
            pairOfUidAndBookIds = analyzingBorrowedBook.loadUserIDAndBookId(Constant.BORROWED_BOOK_UID_BOOK_ID);
            int useLen = 0;
            for (int i = 0; i < pairOfUidAndBookIds.length; i++) {
                if(pairOfUidAndBookIds[i] != null){
                    useLen++;
                }
            }
            borrowCount = useLen;
        }catch (IOException e){
            throw new RuntimeException();
        }
    }
    public void storeUidAndBookId(){
        try {
            analyzingBorrowedBook.storeUserIDAndBookId(pairOfUidAndBookIds, Constant.BORROWED_BOOK_UID_BOOK_ID);
        }catch (IOException e){
            throw new RuntimeException();
        }
    }
}
